package com.gitee.qdbp.jdbc.biz;

import com.gitee.qdbp.able.jdbc.condition.TableJoin;
import com.gitee.qdbp.jdbc.api.SqlBoot;
import com.gitee.qdbp.jdbc.model.DbType;
import com.gitee.qdbp.jdbc.model.DbVersion;
import com.gitee.qdbp.jdbc.plugins.DbPluginHelper;
import com.gitee.qdbp.jdbc.plugins.SqlDialect;
import com.gitee.qdbp.jdbc.sql.SqlBuffer;
import com.gitee.qdbp.jdbc.sql.SqlBuilder;
import com.gitee.qdbp.jdbc.sql.build.CommonSqlTools;
import com.gitee.qdbp.jdbc.sql.build.CrudSqlHelper;
import com.gitee.qdbp.jdbc.sql.build.QuerySqlHelper;
import com.gitee.qdbp.jdbc.sql.parse.SqlFragmentContainer;
import com.gitee.qdbp.jdbc.utils.ParseTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

public class SqlBootImpl implements SqlBoot {

    /** 数据库方言 **/
    private final SqlDialect dialect;
    /** 插件容器 **/
    private final DbPluginHelper plugins;
    /** SQL工具类 **/
    private final CommonSqlTools sqlTools;
    /** SQL容器 **/
    private final SqlFragmentContainer sqlContainer;

    public SqlBootImpl(DbType dbType, DbPluginHelper plugins) {
        this(plugins.buildSqlDialect(dbType), plugins);
    }

    public SqlBootImpl(DbVersion version, DbPluginHelper plugins) {
        this(plugins.buildSqlDialect(version), plugins);
    }

    public SqlBootImpl(SqlDialect dialect, DbPluginHelper plugins) {
        VerifyTools.requireNonNull(dialect, "dialect");
        VerifyTools.requireNonNull(plugins, "plugins");
        this.dialect = dialect;
        this.plugins = plugins;
        this.sqlTools = new CommonSqlTools(this);
        this.sqlContainer = new SqlFragmentContainer(this);
    }

    @Override
    public SqlBuffer newSqlBuffer() {
        return new SqlBuffer(dialect, plugins);
    }

    @Override
    public SqlBuilder newSqlBuilder() {
        return new SqlBuilder(dialect, plugins);
    }

    @Override
    public ParseTools parseTools() {
        return plugins.parseTools();
    }

    @Override
    public CommonSqlTools sqlTools() {
        return sqlTools;
    }

    @Override
    public SqlFragmentContainer sqlContainer() {
        return sqlContainer;
    }

    @Override
    public SqlDialect sqlDialect() {
        return dialect;
    }

    @Override
    public DbPluginHelper plugins() {
        return plugins;
    }

    @Override
    public CrudSqlHelper sqlHelper(Class<?> clazz) {
        return new CrudSqlHelper(clazz, this);
    }

    @Override
    public QuerySqlHelper sqlHelper(TableJoin tables) {
        return new QuerySqlHelper(tables, this);
    }
}
